;;; Copyright © 2018 Kei Kebreau <kkebreau@posteo.net>
;;;
;;; This program is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(define-module (nes)
  #:use-module (guix packages)
  #:use-module (gnu packages)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system scons)
  #:use-module (guix build-system trivial)
  #:use-module (gnu packages bash)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages fontutils)
  #:use-module (gnu packages gd)
  #:use-module (gnu packages gl)
  #:use-module (gnu packages gtk)
  #:use-module (gnu packages lua)
  #:use-module (gnu packages pkg-config)
  #:use-module (gnu packages python)
  #:use-module (gnu packages python-xyz)
  #:use-module (gnu packages sdl))

(define-public cc65
  (package
    (name "cc65")
    (version "2.17")
    (source (origin
              (method url-fetch)
              (uri (string-append "https://github.com/cc65/cc65/archive/V"
                                  version ".tar.gz"))
              (sha256
               (base32 "1b1fdl8vn674hrc0837gwlp90yk5347rbf50kbpnrz2vcls9df3k"))
              (file-name (string-append name "-" version ".tar.gz"))))
    (build-system gnu-build-system)
    (arguments
     '(#:make-flags (list (string-append "PREFIX=" (assoc-ref %outputs "out")))
       #:tests? #f ; no check target
       #:phases
       (modify-phases %standard-phases
         (delete 'configure)))) ; no configure script
    (home-page "https://cc65.github.io/cc65/")
    (synopsis "C compiler for the 6502 microprocessor")
    (description "@code{cc65} is a complete cross development package for
65(C)02 systems, including a powerful macro assembler, a C compiler, linker,
librarian and several other tools.")
    (license license:zlib)))

(define-public fceux
  (package
   (name "fceux")
   (version "2.2.3")
   (source (origin
            (method url-fetch)
            (uri (string-append "mirror://sourceforge/fceultra/Source Code/"
                                version " src/" name "-" version ".src.tar.gz"))
            (sha256
             (base32 "0gl2i3qdmcm7v9m5kpfz98w05d8m33990jiwka043ya7lflxvrjb"))
            (file-name (string-append name "-" version ".tar.gz"))))
   (build-system scons-build-system)
   (arguments
    `(#:tests? #f ; no tests
      #:scons ,scons-python2
      #:scons-flags
      (list (string-append "--prefix=" (assoc-ref %outputs "out"))
            ; Build with GTK+ version 2 GUI support.
            "GTK=1"
            ; Don't use the third-party bundled libraries.
            "SYSTEM_LUA=1"
            "SYSTEM_MINIZIP=1")
      #:phases
      (modify-phases %standard-phases
        (add-after 'unpack 'scons-propagate-environment
          (lambda _
            ;; By design, SCons does not, by default, propagate
            ;; environment variables to subprocesses.  See:
            ;; <http://comments.gmane.org/gmane.linux.distributions.nixos/4969>
            ;; Here, we modify the SConstruct file to arrange for
            ;; environment variables to be propagated.
            (substitute* "SConstruct"
              (("^env = Environment\\(")
               "env = Environment(ENV=os.environ, "))
            #t)))))
   (native-inputs
    `(("pkg-config" ,pkg-config)))
   (inputs
    `(("gd" ,gd)
      ("glu" ,glu)
      ("gtk+" ,gtk+-2)
      ("lua" ,lua-5.1)
      ("minizip" ,minizip)
      ("sdl" ,sdl)
      ("zlib" ,zlib)))
   (home-page "http://www.fceux.com")
   (synopsis
    "Nintendo Entertainment System (NES/Famicom) emulator based on FCE Ultra")
   (description
    "FCEUX is a Nintendo Entertainment System (NES), Famicom, Famicom
Disk System (FDS), and Dendy emulator.  It supports NTSC (USA/JPN), PAL
(European), and NTSC-PAL Hybrid modes.")
   (license license:gpl2+)))

(define-public pilbmp2nes
  (let ((commit "1b75379d30e6d7cf7b6e389e402c56f27f7e57a1")
        (revision "1"))
    (package
      (name "pilbmp2nes")
      (version (git-version "0.0" revision commit))
      (source (origin
                (method git-fetch)
                (uri (git-reference
                      (url "https://github.com/pinobatch/nrom-template")
                      (commit commit)))
                (sha256
                 (base32
                  "0xqvjdsy4953rywnrnsyz5yvkqwmjv865dv5ridavl02am73mzv8"))
                (file-name (git-file-name name version))))
      (build-system trivial-build-system)
      (native-inputs
       `(("source" ,source)))
      (inputs
       `(("bash" ,bash) ; for wrap-program
         ("python" ,python)
         ("python-pillow" ,python-pillow)))
      (arguments
       '(#:modules ((guix build utils))
         #:builder (begin
                     (use-modules (guix build utils))

                     (let* ((source (assoc-ref %build-inputs "source"))
                            (bash (assoc-ref %build-inputs "bash"))
                            (python (string-append
                                     (assoc-ref %build-inputs "python")
                                     "/bin/python3"))
                            (python-pillow
                             (string-append
                              (assoc-ref %build-inputs "python-pillow")
                              "/lib/python3.6/site-packages"))
                            (output (assoc-ref %outputs "out"))
                            (bin (string-append output "/bin"))
                            (executable (string-append bin "/pilbmp2nes")))
                       (mkdir-p bin)
                       (copy-file (string-append source "/tools/pilbmp2nes.py")
                                  executable)
                       ;; Use the correct python interpreter.
                       (substitute* executable
                         (("#!/usr/bin/env python3")
                          (string-append "#!" python)))
                       ;; Ensure that bash is in PATH so wrap-program can detect
                       ;; it.
                       (setenv "PATH" (string-append bash "/bin"))
                       (wrap-program executable
                         `("PYTHONPATH" ":" prefix (,python-pillow)))
                       #t))))
      (home-page "https://github.com/pinobatch/nrom-template")
      (synopsis "Bitmap to multi-console CHR converter using Pillow")
      (description
       "@code{pilbmp2nes} is a command-line converter from indexed BMP or PNG to
multiple video game consoles' tile formats.")
      (license
       (license:non-copyleft
        ;; GNU All-Permissive License
        "https://www.gnu.org/prep/maintain/html_node/License-Notices-for-Other-Files.html")))))
